package com.yimils.core.goods.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yimils.core.goods.entity.GoodsPersonEntity;
import com.yimils.core.goods.entity.PersonEntity;
import com.yimils.core.goods.mapper.GoodsPersonMapper;
import com.yimils.core.goods.serivce.GoodsPersonService;
import com.yimils.core.goods.serivce.PersonService;
import com.yimils.core.goods.vo.GoodsPersonRes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("goodsPersonService")
public class GoodsPersonServiceImpl extends ServiceImpl<GoodsPersonMapper, GoodsPersonEntity> implements GoodsPersonService {
    @Resource(name = "personService")
    PersonService personService;

    @Override
    public Boolean saveGoodsPerson(List<Long> personIds, Long goodsId) {
        List<GoodsPersonEntity> collect = personIds.stream().map(o -> {
            GoodsPersonEntity goodsPersonEntity = new GoodsPersonEntity();
            goodsPersonEntity.setGoodsId(goodsId);
            goodsPersonEntity.setPersonId(o);
            goodsPersonEntity.setState(Boolean.FALSE);
            goodsPersonEntity.setCreateTime(LocalDateTime.now());
            goodsPersonEntity.setUpdateTime(LocalDateTime.now());
            return goodsPersonEntity;
        }).collect(Collectors.toList());
        return saveBatch(collect);
    }

    @Override
    public Boolean deleteGoodsPerson(List<Long> personIds, Long goodsId) {
        QueryWrapper<GoodsPersonEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(GoodsPersonEntity::getGoodsId, goodsId)
                .eq(GoodsPersonEntity::getState, Boolean.FALSE);
        List<GoodsPersonEntity> entities = list(qw);
        entities.forEach(o -> {
            o.setState(Boolean.TRUE);
            o.setUpdateTime(LocalDateTime.now());
        });

        return saveGoodsPerson(personIds, goodsId);
    }

    @Override
    public Map<Long, List<GoodsPersonRes>> mapListGoodsPerson(List<Long> goodsIds) {
        QueryWrapper<GoodsPersonEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(GoodsPersonEntity::getGoodsId, goodsIds)
                .eq(GoodsPersonEntity::getState, Boolean.FALSE);
        List<GoodsPersonEntity> entities = list(qw);

        List<Long> personIds = entities.stream().map(GoodsPersonEntity::getPersonId).collect(Collectors.toList());
        List<PersonEntity> pictureEntities = personService.listPerson(personIds);
        Map<Long, String> videoMap = pictureEntities.stream().collect(Collectors.toMap(PersonEntity::getId, PersonEntity::getRightPersonName));
        return entities.stream().map(o -> {
            GoodsPersonRes res = new GoodsPersonRes();
            res.setGoodsId(o.getGoodsId());
            res.setPersonId(o.getPersonId());
            res.setRightPersonName(videoMap.get(o.getId()));
            return res;
        }).collect(Collectors.groupingBy(GoodsPersonRes::getGoodsId));
    }
}
